/*
 * Created by JFormDesigner on Fri Apr 15 21:53:07 CST 2022
 */

package com.ZengQi.event.view;

import java.awt.event.*;

import com.ZengQi.event.controller.UpdateCompanyController;
import com.ZengQi.event.entity.Admin;
import com.ZengQi.event.entity.Company;

import java.awt.*;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author QI Zeng
 */
public class UpdateCompanyView extends JFrame {
    private UpdateCompanyController updateCompanyController=new UpdateCompanyController();
    private String oldCompanyName;
    boolean judge;
    public UpdateCompanyView(Company company,String oldCompanyName) {
        this.oldCompanyName=oldCompanyName;
        initComponents();
        companyTxt.setText(company.getCompanyName());
        companyKey.setText(company.getCompanyKey());
        companyArea1.setText(company.getCompanyDescription());
        setVisible(true);
        setLocationRelativeTo(null);


    }

    private void updateBtn(ActionEvent e) {
        updateBtnActionPerformed(e);
    }

    private void updateBtnActionPerformed(ActionEvent e) {
        update();
    }

    private void update() {
        Company company=new Company();
        company.setCompanyName(companyTxt.getText());
        company.setCompanyKey(companyKey.getText());
        company.setCompanyDescription(companyArea1.getText());

        Admin admin=new Admin();
        admin.setAdminCompany(companyTxt.getText());

       judge= updateCompanyController.update(company,admin,oldCompanyName);
        if(judge){
            JOptionPane.showMessageDialog(null, "修改成功！");
            dispose();
        }else {
            JOptionPane.showMessageDialog(null, "数据库出现了问题，请联系管理员，修改失败了~！");
            dispose();
        }

    }

    /**
     * 组件布局初始化
     */
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        label1 = new JLabel();
        companyTxt = new JTextField();
        label2 = new JLabel();
        companyKey = new JTextField();
        label3 = new JLabel();
        scrollPane1 = new JScrollPane();
        companyArea1 = new JTextArea();
        button1 = new JButton();

        //======== this ========
        setTitle("\u505a\u9898\u5e2e");
        setIconImage(new ImageIcon(getClass().getResource("/bang.png")).getImage());
        Container contentPane = getContentPane();

        //======== panel1 ========
        {

            //---- label1 ----
            label1.setText("\u4f01\u4e1a\u540d\u79f0");

            //---- label2 ----
            label2.setText("\u4f01\u4e1a\u8003\u6838\u8981\u70b9");

            //---- label3 ----
            label3.setText("\u4f01\u4e1a\u7b80\u4ecb");

            //======== scrollPane1 ========
            {
                scrollPane1.setViewportView(companyArea1);
            }

            //---- button1 ----
            button1.setText("\u786e\u8ba4\u4fee\u6539");
            button1.addActionListener(e -> updateBtn(e));

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(60, 60, 60)
                                .addGroup(panel1Layout.createParallelGroup()
                                    .addGroup(panel1Layout.createSequentialGroup()
                                        .addGroup(panel1Layout.createParallelGroup()
                                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
                                            .addComponent(label2, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE))
                                        .addGap(18, 18, 18)
                                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                            .addComponent(companyKey, GroupLayout.PREFERRED_SIZE, 161, GroupLayout.PREFERRED_SIZE)
                                            .addComponent(companyTxt, GroupLayout.PREFERRED_SIZE, 161, GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(panel1Layout.createSequentialGroup()
                                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
                                        .addGap(27, 27, 27)
                                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 331, GroupLayout.PREFERRED_SIZE))))
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(289, 289, 289)
                                .addComponent(button1)))
                        .addContainerGap(262, Short.MAX_VALUE))
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label1)
                            .addComponent(companyTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGap(32, 32, 32)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(companyKey, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label2))
                        .addGap(37, 37, 37)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addComponent(label3)
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
                        .addGap(35, 35, 35)
                        .addComponent(button1)
                        .addContainerGap(29, Short.MAX_VALUE))
            );
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel label1;
    private JTextField companyTxt;
    private JLabel label2;
    private JTextField companyKey;
    private JLabel label3;
    private JScrollPane scrollPane1;
    private JTextArea companyArea1;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
